﻿
Public Module tcptalk_app
    Private Sub printhelp()
        raiseError("usage tcptalk.exe host:port")
    End Sub

    Public Sub main(ByVal args() As String)
        counter.initial(counter.NOCOUNTERFILEMARK)
        If args Is Nothing OrElse args.Length = 0 Then
            printhelp()
            Return
        End If

        Dim host As String = Nothing
        Dim port As Int32 = 0
        If Not parseHostPort(args(0), host, port) Then
            printhelp()
            Return
        End If

        Dim talk As tcpTalk = Nothing
        talk = New tcpTalk()
        talk.host = host
        talk.port = port

        While Not consoleCancelKeyPressed()
            talk.sendData.clear()
            Dim input As String = Nothing
            input = Console.ReadLine()
            While Not input Is Nothing
                talk.sendData.insert(input)
                input = Console.ReadLine()
            End While

            If Not talk.talk() Then
                raiseError("cannot talk with server " + args(0), errorHandle.errorType.warning)
            ElseIf talk.receiveData().size() = 0 Then
                raiseError("talk success, but without response.")
            Else
                Dim msg As String = Nothing
                msg = "success, response data is"
                For i As Int64 = 0 To talk.receiveData().size() - 1
                    Dim s As String = Nothing
                    If Not talk.receiveData().take(i, s) Then
                        raiseError("cannot get response data index " + Convert.ToString(i), _
                                   errorHandle.errorType.warning)
                    Else
                        msg += newline.incode() + s
                    End If
                Next

                raiseError(msg)
            End If
        End While
    End Sub
End Module
